package com.github.admin.edu.security.jwt.server.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.entity.JsonTree;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.common.util.ObjectUtil;
import com.github.admin.edu.assembly.string.util.StringUtils;
import com.github.admin.edu.orm.serivce.impl.DataRestfulAbstractBaseService;
import com.github.admin.edu.security.jwt.server.domain.ServerOrganizationDomain;
import com.github.admin.edu.security.jwt.server.entity.TSysOauthManage;
import com.github.admin.edu.security.jwt.server.entity.TSysOrginzation;
import com.github.admin.edu.security.jwt.server.service.ServerOrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Optional;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-1
 */
@Service
@Slf4j
public class ServerOrganizationServiceImpl extends DataRestfulAbstractBaseService<TSysOrginzation,Integer>
        implements ServerOrganizationService {

    @Autowired
    private ServerOrganizationDomain domain;

    public String delete(String ids) {
        return super.delete(ids,"Long");

    }
    @Override
    public String getALLPostByPager(String jsonEntity) {
        if(StringUtils.isNotBlank(jsonEntity)){
            JsonEntity<TSysOrginzation> tJsonEntity= JsonUtils.toCollection(jsonEntity,
                    new TypeReference<JsonEntity<TSysOrginzation>>() {
            });
            if(null!=tJsonEntity){
                return super.getALLPostByPager(tJsonEntity.getData(),tJsonEntity.getPager());
            }
        }
        return JsonUtils.toJson(new JsonEntity<TSysOrginzation>("未找到参数！"));
    }

    public String getEntity(Integer id){
        if(null!=id){
            JsonEntity jsonEntity=new JsonEntity<>();
            Optional<TSysOrginzation> optional=domain.findById(id);
            if(optional.isPresent()){
                TSysOrginzation t=optional.get();
                if(null!=t){
                    Optional<TSysOrginzation> p=domain.findById(t.getParentid());
                    if(p.isPresent()){
                        TSysOrginzation pt=p.get();
                        t.setParentName(pt.getName());
                    }
                    jsonEntity.setData(t);
                }
                return JsonUtils.toJson(jsonEntity);
            }
            return JsonUtils.toJson(new JsonEntity<TSysOrginzation>("没有查询到对象"));
        }
        return JsonUtils.toJson(new JsonEntity<TSysOrginzation>("没有ID值"));
    }
    @Override
    public String getAllGetEntity(String title, Integer current, Integer rows, String order, String fileId) {
        TSysOrginzation tSysUser=new TSysOrginzation();
        if(StringUtils.isNotBlank(title)){
            tSysUser.setName(title);
        }
        return super.getAllGetEntity(tSysUser,current,rows,order,fileId);
    }

    @Override
    public String getAllEntity(String title, String order, String fileId) {
        return getAllGetEntity(title,0,0,order,fileId);
    }

    @Transactional(readOnly = true)
    @Override
    public String getAllByTree() {
        List<TSysOrginzation> list=domain.getAll();
        JsonTree<TSysOrginzation> jsonTree=new JsonTree<>();
        jsonTree.setData(list);
        return JsonUtils.toJson(jsonTree);
    }


    @Transactional
    @Override
    public String deleteBYRootId(Integer id) {
        Optional<TSysOrginzation> optional=domain.findById(id);
        if(optional.isPresent()){
            TSysOrginzation orginzation=optional.get();
            domain.deleteAllByTreeCode(orginzation.getTreeCode()+"-%");
            domain.delete(orginzation);
            return JsonUtils.toJson(new JsonEntity<TSysOrginzation>());
        }
        return JsonUtils.toJson(new JsonTree<TSysOrginzation>("对象不存在！"));
    }

    @Override
    public String findOrgByFirst() {
        List<TSysOrginzation> list=domain.getByFirstRoot();
        if(null!=list&&list.size()>0){
            TSysOrginzation orginzation=list.get(0);
            JsonEntity<TSysOrginzation> jsonEntity=new JsonEntity<>();
            jsonEntity.setData(orginzation);
            return JsonUtils.toJson(jsonEntity);
        }
        return JsonUtils.toJson(new JsonEntity<TSysOrginzation>());
    }

    /**
     * 保存或者更新
     * @param jsonEntity
     * @return
     */
    @Transactional
    public String saveOrUpdate(String jsonEntity){
        if(StringUtils.isNotBlank(jsonEntity)){
            JsonEntity<TSysOrginzation> tJsonEntity=JsonUtils.toCollection(jsonEntity, new TypeReference<JsonEntity<TSysOrginzation>>() {
            });
            TSysOrginzation t=tJsonEntity.getData();
            Object o= ObjectUtil.getValueByKey(t,"id");
            if(org.springframework.util.StringUtils.isEmpty(o)){
                Field f = null;
                try {
                    f = t.getClass().getDeclaredField("id");
                    f.setAccessible(true);
                    f.set(t,getId(t.getClass().getSimpleName()));
                } catch (NoSuchFieldException e) {
                    log.info("error!!"+e.getMessage());
                    return JsonUtils.toJson(new JsonEntity<TSysOrginzation>("反序列化错误！"));
                } catch (IllegalAccessException e) {
                    log.info("error!!"+e.getMessage());
                    return JsonUtils.toJson(new JsonEntity<TSysOrginzation>("反序列化错误！"));
                }
            }
            t=domain.save(t);
            tJsonEntity.setData(t);
            return JsonUtils.toJson(tJsonEntity);
        }
        return JsonUtils.toJson(new JsonEntity<TSysOrginzation>("保存对象为空！"));
    }
}
